<?php
/**
 * Author Yulin
 * Email  dwzhanglong@126.com
 * Date   2017-7-21
 * Func   首页入口
 */
defined('IN_YuLin') || exit('NO PERMIT!');

// ================= 替换文本信息准备 ============= 
// 当前用户等级
$jinJiLevel_str   = '未登录';
// 可领取奖励
$jinJiReward_str  = '0';
// 领取通道按钮
$rewardLingqu_str = '<a class="disable">不可领取</a>';
// 所有等级设置晋级图
$levelStr         = '';
// 昨日投注数额
$yesterdayBetAmount = '0.00';

$levelTab         = Table('user_level');
$mainTab          = Table('lottery_order_main');

$sql              = 'SELECT id, level, title, group_point, reward, reward FROM ' . $levelTab . ' WHERE status = 1 ORDER BY group_point ASC';
$levelData = $db->getall($sql);
$levelIdKey= array();
foreach ($levelData as $k => $v){
    $levelIdKey[$v['id']] = $v;
}

if ($a == 'reward' && $_userInfo){
    $b_level = '';
    $e_level = '';
    $last_take_level = $_userInfo['last_take_prize_level'] ? $_userInfo['last_take_prize_level'] : 0;
    foreach ($levelIdKey as $k => $v){
        if ($k > $last_take_level && $last_take_level < $_userInfo['level_id']){
            $last_take_level = $k;
            if (!$b_level) $b_level = $v['level'];
            $jinJiReward_str += $v['reward'];
            $e_level = $v['level'];
        }
    }
    // 等级奖励，并更新领取记录状态
    $weimiao         = microtime(true);
    $lkey            = 'SJJL' . date('YmdHis') . str_pad(substr($weimiao, strpos($weimiao, '.') + 1), '4', 0);
    $data['money']   = $jinJiReward_str;
    $data['uid']     = $_user['id'];
    $data['balance'] = +$jinJiReward_str;
    $data['tid']     = 'jinji_jiangli';
    $data['lkey']    = $lkey;
    $data['notes']   = '晋级奖励：[' . $b_level . ' 到 ' . $e_level . ']' . $jinJiReward_str . '元';
    $flag = MoneyLog($data);
    if ($flag){
        $sql = 'UPDATE ' . Table('user_info') . ' SET last_take_prize_level = ' . $last_take_level . ' WHERE uid = ' . $_user['id'];
        $db->exec($sql);
    }
    ShowMsg('领取成功', U('index/activity'));
    return;
}

$table = Table('activity');
$lists = $db->getall("select `title`,`desc`,`thumb`,`isPic`,`contents` from ".$table . ' WHERE source = 1');
foreach ($levelData as $k => $v){
    $levelStr .= '<tr><td>' . $v['level'] . '</td><td>' . $v['title'] . '</td><td>' . $v['group_point'] . '</td><td>' . $v['reward'] . '</td><td>' . $v['reward'] . '</td></tr>';
}

if ($_user){
    // 如果有登录， 设置活动页面上登录状态， 等级，以及晋级奖励的领取状态
    $jinJiLevel_str   = $levelIdKey[$_userInfo['level_id']]['level'];
    if ($levelIdKey[$_userInfo['level_id']]['reward'] != 0 && $_userInfo['level_id'] != $_userInfo['last_take_prize_level']){
        // 如果当前等级有奖励设置（reward）并且上次领取的等级不等于当前等级， 则表示有奖励可以领取
        $last_take_level = $_userInfo['last_take_prize_level'] ? $_userInfo['last_take_prize_level'] : 0;
        foreach ($levelIdKey as $k => $v){
            if ($k > $last_take_level && $last_take_level < $_userInfo['level_id']){
                $last_take_level = $k;
                $jinJiReward_str += $v['reward'];
            }
        }
        $rewardLingqu_str = '<a class="" href="?m=index&c=activity&a=reward">领取奖励</a>';
    }
    $sql = 'SELECT SUM(amount) as y_amount FROM ' . $mainTab . ' WHERE createtime > ' . strtotime(date('Ymd', strtotime('-1 day'))) . ' AND createtime < ' . strtotime(date('Ymd'));
    $orderAmountData = $db->getrow($sql);
    if($orderAmountData['y_amount'] > 0){
        $yesterdayBetAmount = $orderAmountData['y_amount'];
    }
}

// 晋级奖励部分重设
foreach ($lists as $k => $v){
    if (strpos($v['contents'], '[LEVEL_RULE]') !== false){
        $v['contents'] = str_replace('[LEVEL_RULE]', $levelStr, $v['contents']);
    }
    if (strpos($v['contents'], '[USER_LEVEL]') !== false){
        $v['contents'] = str_replace('[USER_LEVEL]', $jinJiLevel_str, $v['contents']);
    }
    if (strpos($v['contents'], '[LEVEL_REWARD]') !== false){
        $v['contents'] = str_replace('[LEVEL_REWARD]', $jinJiReward_str, $v['contents']);
    }
    if (strpos($v['contents'], '[TAKE_STATUS]') !== false){
        $v['contents'] = str_replace('[TAKE_STATUS]', $rewardLingqu_str, $v['contents']);
    }
    if (strpos($v['contents'], '[LAST_DAY_BET_AMOUNT]') !== false){
        $v['contents'] = str_replace('[LAST_DAY_BET_AMOUNT]', $yesterdayBetAmount, $v['contents']);
    }
    $lists[$k] = $v;
}

$tpl->display('index/activity');